package com.wu.foodsafetyplayer.utils.datepicker;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 说明：日期格式化工具 作者：liuwan1992 添加时间：2018/12/17 修改人：liuwan1992 修改时间：2018/12/18
 */
public class DateFormatUtils {

	private static final String DATE_FORMAT_PATTERN_YMD = "yyyy-MM-dd";
	private static final String DATE_FORMAT_PATTERN_HMS = "HH:mm:ss.SSS";
	private static final String DATE_FORMAT_PATTERN_YMD_HM = "yyyyMMdd_HHmm";
	private static final String DATE_FORMAT_PATTERN_YMD_HM2 = "yyyy-MM-dd HH:mm";
	private static final String DATE_FORMAT_PATTERN_YMD_HMS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
	private static final String DATE_FORMAT_PATTERN_D_HM = "dd HH:mm";

	/**
	 * 时间戳转字符串
	 * 
	 * @param timestamp
	 *            时间戳
	 * @param isPreciseTime
	 *            是否包含时分
	 * @return 格式化的日期字符串
	 */
	public static String long2Str(long timestamp, boolean isPreciseTime) {
		return long2Str(timestamp, getFormatPattern(isPreciseTime));
	}

	private static String long2Str(long timestamp, String pattern) {
		return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(
				timestamp));
	}

	/**
	 * 字符串转时间戳
	 * 
	 * @param dateStr
	 *            日期字符串
	 * @param isPreciseTime
	 *            是否包含时分
	 * @return 时间戳
	 */
	public static long str2Long(String dateStr, boolean isPreciseTime) {
		return str2Long(dateStr, getFormatPattern(isPreciseTime));
	}

	private static long str2Long(String dateStr, String pattern) {
		try {
			return new SimpleDateFormat(pattern, Locale.CHINA).parse(dateStr)
					.getTime();
		} catch (Throwable ignored) {
		}
		return 0;
	}

	private static String getFormatPattern(boolean showSpecificTime) {
		if (showSpecificTime) {
			return DATE_FORMAT_PATTERN_YMD_HM;
		} else {
			return DATE_FORMAT_PATTERN_D_HM;
		}
	}

	/**
	 * 北京时间转格林时间 String
	 * 
	 * @param data
	 * @return
	 */
	public static String Data2GMT(String data) {
		return long2Str(str2Long(data, true) - (8 * 3600 * 1000), true);
	}

	/**
	 * 格林str时间转long
	 * 
	 * @param dateStr
	 * @return
	 */
	public static long str2Long(String dateStr) {
		try {
			return new SimpleDateFormat(DATE_FORMAT_PATTERN_YMD, Locale.CHINA)
					.parse(dateStr).getTime();
		} catch (Throwable ignored) {
		}
		return 0;
	}

	public static long str2Long2(String dateStr) {
		try {
			return new SimpleDateFormat(DATE_FORMAT_PATTERN_HMS, Locale.CHINA)
					.parse(dateStr).getTime();
		} catch (Throwable ignored) {
		}
		return 0;
	}

	public static String long2Str(long timestamp) {
		timestamp = timestamp - (8 * 3600 * 1000);
		return new SimpleDateFormat(DATE_FORMAT_PATTERN_YMD_HMS, Locale.CHINA)
				.format(new Date(timestamp));
	}
}
